import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.LinkedList;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.plugin.Thresholder;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import ij.process.ImageConverter;
import extractLineUtils.*;
import fr.irstv.dataModel.Segment;


public class ExtractLine implements PlugInFilter {

		//parametres Canny
		float alphaD = 1;
		//parametres Hough
		int phi_resolution = 256;
		int rad_resolution = 256;
		int lineNb = 20 ; //nbr de lignes extraites

		//parametres du plug-in
		public static String imagePath = "/Users/mservieres/Documents/recherche/Mazad/AppliVisu/Resultats_mazad/";
		//protected String imageName;
		//protected String suffix = new String(".tif");
		protected LinkedList lines = new LinkedList(); //ligne extraites de l'image
		protected String outPath;
		protected ImagePlus imp;
		
		/*public int setup(String arg0, ImagePlus imp) {
			if (arg0.equals("about")) {
	            showAbout();
	            return DONE;
	        }
	       else {
	    	   if (arg0.length() == 0) {
	    		   IJ.showMessage("An image name (without suffix) is required as argument!");
	    	   }
	    	   else {
	    		   imageName = arg0;
	    	   }
	       }
		return NO_IMAGE_REQUIRED;
		}*/
		public int setup(String arg, ImagePlus imp) {
			if (arg.equals("about"))
				{showAbout(); return DONE;}
			this.imp = imp;
			return DOES_ALL + NO_CHANGES + NO_UNDO;
		}

		public void run(ImageProcessor ip) {
			
			//ImagePlus imp = new ImagePlus(imagePath+suffix);
			
			//creer l'image a utiliser pour les calculs
			ImagePlus imageToProcess = new ImagePlus(imp.getTitle()+" to process",imp.getProcessor());
			imageToProcess.show();
			
			//convertir l'image en 8bit
			ImageConverter imageConverter = new ImageConverter(imageToProcess);
			imageConverter.convertToGray8();
			imageToProcess.show();
			
			// appliquer le filtre de Canny-Deriche
			imageToProcess = Deriche_EL.processDeriche(imageToProcess, alphaD);
			imageToProcess.show();
			
			//passer en binaire
			Thresholder thres = new Thresholder();
			//thres.applyThreshold(imageToProcess);
			thres.run("mask");
			//imageToProcess = WindowManager.getCurrentImage();
			imageToProcess.show();
			
			//transformee de Hough
			lines = LinearHoughTransform_EL.processHough(imageToProcess, phi_resolution, rad_resolution, lineNb);
			
			//retransformer image to process en RGB
			imageConverter = new ImageConverter(imageToProcess);
			imageConverter.convertToRGB();
			//ecrire les lignes sur l'image 
			showLines(imp, lines);
			showLines(imageToProcess, lines);
			imageToProcess.updateAndDraw();
			//ecrire les lignes dans un fichier
			writeLines(lines, imagePath+imp.getTitle()+"segments.txt");
		}
		
		public void showLines(ImagePlus imp, LinkedList lines){
			imp.getProcessor().setColor(Color.red);
			//for(Segment seg : lines){
			for(int i=0; i<lines.size(); i++){
				Segment seg = (Segment)lines.get(i);
				imp.getProcessor().drawLine((int)seg.getBeginPoint().get(0),(int) seg.getBeginPoint().get(1), (int) seg.getEndPoint().get(0), (int)seg.getEndPoint().get(1));
			}
			imp.updateAndDraw();
		}
		
		public void writeLines(LinkedList lines, String outPath){
			//String outPath = imagePath+imageName+"Lines.txt";
			File file= new File(outPath);

			try {
				PrintWriter fout = new PrintWriter( new FileOutputStream(file));
				for(int i=0;i<lines.size();i++) {
		        	Segment seg = (Segment)(lines.get(i));
		        	fout.println(seg.getBeginPoint().get(0)+" "+seg.getBeginPoint().get(1)+" "+seg.getEndPoint().get(0)+" "+seg.getEndPoint().get(1));
		        }
		        fout.close();
			} catch (FileNotFoundException e) {
				System.out.println("erreur d'ecriture des segments dans le fichier de sortie "+outPath);
				e.printStackTrace();
			}
	        
			
		}
		
		//public void writeLines(String filename, LinkedList<Segment>lines)
		
		public void showAbout() {
			IJ.showMessage("Extract line from image...",
				" "
			);
		}
}
